package com.team7.happycommunity.propertymanagement.controller;

import com.github.pagehelper.PageInfo;
import com.team7.happycommunity.propertymanagement.common.CommonResult;
import com.team7.happycommunity.propertymanagement.pojo.Announcement;
import com.team7.happycommunity.propertymanagement.service.AnnoncementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/annoncement")
public class AnnoncementController {

    @Autowired
    AnnoncementService annoncementService;

    @PostMapping("/add")
    public CommonResult addAnnoncement(@RequestBody Announcement announcement){
        Boolean result = annoncementService.save(announcement);
        if(result){
            return CommonResult.success("存储成功");
        }else{
            return CommonResult.failed("存储失败");
        }
    }

    @PostMapping("/del")
    public CommonResult delAnnoncement(Integer id){
        int result = annoncementService.delete(id);
        return result==0?CommonResult.failed():CommonResult.success();
    }

    @GetMapping("/list")
    public CommonResult getAnnoncements(@RequestParam(value="page",defaultValue = "1",required = false) Integer currentPage,
                                        @RequestParam(value="limit",defaultValue = "5",required = false) Integer pageSize,
                                        @RequestParam(value = "username",defaultValue = "null",required = false) String username){
        PageInfo info = null;
        try{
            List<Announcement> list = annoncementService.findAnnoncementByPage(currentPage,pageSize,username);
            info = new PageInfo(list);
            return CommonResult.success(info);
        }catch (Exception e){
            e.printStackTrace();
            return CommonResult.failed();
        }
    }
}
